import {EasyACtx} from "./EasyACtx";
import {Log} from "../util/Log";

export class EasyACtxHandle{
    public forceContinue = false; //强制执行  ---无视 isEnd 的值直接运行

    public from;//来源 (Controller 或 Interceptor)

    constructor(public handleFunc:((ctx:EasyACtx,handle?) =>void)){

    }

    async doHandle(ctx:EasyACtx){

        delete ctx.handle;
        if(!ctx.isEnd || this.forceContinue){
            try {
                ctx.handle = this;
                await this.handleFunc(ctx,this);
            }catch (err){
                Log.err(err);
                console.error(err.stack);
                ctx.isEnd = true;
                ctx.setcm(500,'Internal Server Error ');
                ctx.next();
            }
        }else {
             ctx.next();
        }
    }



}

